package me.zhengjie.modules.equipment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.equipment.domain.TEqpttype;
import me.zhengjie.modules.equipment.service.TEqpttypeService;
import me.zhengjie.modules.equipment.service.dto.TEqpttypeQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author sw
* @date 2019-07-19
*/
@RestController
@RequestMapping("api")
public class TEqpttypeController {

    @Autowired
    private TEqpttypeService tEqpttypeService;

    @Log("查询TEqpttype")
    @GetMapping(value = "/tEqpttype")
    @PreAuthorize("hasAnyRole('ADMIN','TEQPTTYPE_ALL','TEQPTTYPE_SELECT')")
    public ResponseEntity getTEqpttypes(TEqpttypeQueryCriteria criteria, Pageable pageable){
        try {
            return new ResponseEntity(tEqpttypeService.queryAll(criteria, pageable), HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("新增TEqpttype")
    @PostMapping(value = "/tEqpttype")
    @PreAuthorize("hasAnyRole('ADMIN','TEQPTTYPE_ALL','TEQPTTYPE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TEqpttype resources){
        try {
            return new ResponseEntity(tEqpttypeService.create(resources),HttpStatus.CREATED);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("修改TEqpttype")
    @PutMapping(value = "/tEqpttype")
    @PreAuthorize("hasAnyRole('ADMIN','TEQPTTYPE_ALL','TEQPTTYPE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TEqpttype resources){
        tEqpttypeService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TEqpttype")
    @DeleteMapping(value = "/tEqpttype/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TEQPTTYPE_ALL','TEQPTTYPE_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        tEqpttypeService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}